# coding:utf-8
from mathlib import wavread, wavwrite
from OPAP.tfa import Tfa
from numpy.ma.core import size
from numpy.core.numeric import reshape
from easygui import fileopenbox, filesavebox
import sys

if __name__ == '__main__':
    
    wavFile = fileopenbox(title=u"ファイル選択", default="*.wav")
    x, fs, bits = wavread(wavFile, False)
    
    #信号のパラメータを確認
    print u'信号長:', size(x, 0)
    print u'サンプリング周波数:', fs, '[Hz]'
    print u'量子化ビット数', bits
    
    if size(x, 1) == 2:
        x = x.flatten()[:x.size / 2] #1チャンネル分だけ取り出す
    else:
        x = x.flatten()
    
    #tfaを実行
    try:
        FrameNumber = int(raw_input('FrameNumber >>> '))
    except:
        print 'number is required'
        sys.exit()
    y = Tfa(x, fs, FrameNumber).calc()
    y = reshape(y, (y.size, 1))
    wavFile = filesavebox(title=u"ファイル選択", default=".wav")
    wavwrite(y, fs, bits, wavFile, False)
    